import User from '@/store/user'
import * as toast from '@/util/toast/toast'
import * as hexUtil from './hexUtil'
import * as btViewmodel from './btViewmodel'


///读取全部数据
export function getDeviceAllDataWithComlete(calllback:any){

    if(User.isShowIntoDeviceLoading === true){
        toast.showLoading('加载中')
        setTimeout(() => {
            toast.hideLoading()
        }, 3000);
    }
  

    ///获取所有变量范围
    getDeviceVariableRangeDataWithComplete(function(res1:any){
        calllback(false)
        setTimeout(() => {
             ///获取电压等级范围
        getDeviceVoltageLevelWithComplete(function(res2:any){
            calllback(false)

           setTimeout(() => {
             ///获取使能变量状态范围
             getDeviceDataStatusWithComplete(function(res3:any){
                calllback(false)

               setTimeout(() => {
                 ///获取蓝牙版本范围
                 getDeviceBluetoothVersionWithComplete(function(res4:any){
                    calllback(false)

                    setTimeout(() => {
                        ///获取控制器版本号
                        getDeviceControlVersionWithComplete(function(res5:any) {
                            calllback(false)

                            setTimeout(() => {
                                //获取实时数据
                                getDeviceRuntimeDataWithcomplete(function (res6:any) {
                                   
                                    calllback(true)
                                   

                                })
                           }, 250);
                        })
                   }, 250);
                      
                })
               }, 250);
            })
           }, 250);
        })
    }, 250);
       
    })
}



///获取所有变量范围
export function getDeviceVariableRangeDataWithComplete(calllback:any){
  resetAllTempString()
  sendCMD('BLE_COMMEND_GET_VARIABLE_RANGE_DATA',calllback)
}

///获取电压等级范围
export function getDeviceVoltageLevelWithComplete(calllback:any){
    resetAllTempString()
    sendCMD('BLE_COMMEND_GET_VOLTAGE_LEVEL',calllback)
  }

  
///获取使能变量状态范围
export function getDeviceDataStatusWithComplete(calllback:any){
    resetAllTempString()
    sendCMD('BLE_COMMEND_GET_DEVICE_DATA_STATUS',function(res:boolean){
      calllback(res)
    })
  }
///获取控制器版本范围
export function getDeviceControlVersionWithComplete(calllback:any){
    resetAllTempString()
    sendCMD('BLE_COMMEND_GET_CONTROL_VERSION',calllback)
  }
  
///获取蓝牙版本范围
export function getDeviceBluetoothVersionWithComplete(calllback:any){
    resetAllTempString()
    sendCMD('BLE_COMMEND_GET_BLE_VERSION',calllback)
  }
  
///获取设备实时数据
export function getDeviceRuntimeDataWithcomplete(calllback:any){
    resetAllTempString()
    sendCMD('BLE_COMMEND_GET_RUNTIME_DATA',calllback)
}
  

///获取蓝牙设备唯一id
export function getDeviceUnionIDWithcomplete(calllback:any){
    User.bluetoothCallbackData.string_device_union_id_data = ''
    sendCMD('BLE_COMMEND_GET_UNIONID',calllback)
  }
  

///发送设置电压的指令
export function sendVoltageBleCommend(commend:string,data:string,callback:any){
    console.log('设置电压原始数据：'+data)

    if(judgeCanSendCmdWhenTrailTimeIsOvered() === false){
        return;
    }

   let value = (parseFloat(data) * 10).toString(16);
   console.log('设置电压十六进制数据：'+value)

   if(value.length < 1)
   {
       return;
   }
   let hexStr = ''
   switch (value.length) {
    case 1:
        hexStr = '000' + value
        break;
    case 2:
        hexStr = '00' + value
        break;
    case 3:
        hexStr = '0' + value
        break;
    default:
        break;
   }

   if(hexStr.length == 4)
   {
       let vh = hexStr.slice(0, 2);
       let vl = hexStr.slice(2, 4);
       let bleCommend = getCMDStr(commend);

       
       bleCommend = bleCommend.replace("VL",vl);

       bleCommend = bleCommend.replace("VH",vh);



       
      let array = bleCommend.split(' ');
       let tmpList = [] as any;
       for(let i=0; i<array.length; i++)
       {
           let str = array[i];
           if(str === "s"){
            break
           }else
           {
            tmpList.push(str)
           }
       }

       let s =  hexUtil.hexStrAddByEveryTwoWord(tmpList);

        if(s.length > 2){
            s = s.slice(s.length-2,s.length)
        }
       
       bleCommend = bleCommend.replace('s',s)
       let tmpble = bleCommend.split(' ');
       let bleCommendStr = tmpble.join('')


       const buffer =  hexUtil.hexToArrayBuffer(bleCommendStr);

       
       setTimeout(() => {
            sendBleComd(commend,bleCommendStr,buffer as any,callback);
        }, 100);

        setTimeout(() => {
            sendBleComd(commend,bleCommendStr,buffer as any,callback);
        }, 200);

   }

}



// ///发送设置电压等级的指令
export function sendVoltageLevelBleCommend(commend:string,data:string,callback:any){


    if(judgeCanSendCmdWhenTrailTimeIsOvered() === false){
        return;
    }

    let value = (parseFloat(data) * 10).toString(16);
    if(value.length < 1)
    {
        return;
    }
    let hexStr = ''
    switch (value.length) {
     case 1:
         hexStr = '000' + value
         break;
     case 2:
         hexStr = '00' + value
         break;
     case 3:
         hexStr = '0' + value
         break;
     default:
         break;
    }
    
    
    if(hexStr.length == 4)
    {
        let vh = hexStr.slice(0, 2).toUpperCase();
        let vl = hexStr.slice(0, 4).toUpperCase();
        let bleCommend = getCMDStr(commend);
        
        bleCommend = bleCommend.replace("VL",vl);
        bleCommend = bleCommend.replace("VH",vh);
        
        let array = bleCommend.split(' ');
       let tmpList = [] as any;
       for(let i=0; i<array.length; i++)
       {
           let str = array[i];
           if(str === "s"){
            break
           }else
           {
            tmpList.push(str)
           }
       }

       let s =  hexUtil.hexStrAddByEveryTwoWord(tmpList);

        if(s.length > 2){
            s = s.slice(s.length-2,s.length)
        }
       
       bleCommend = bleCommend.replace('s',s)
       let tmpble = bleCommend.split(' ');
       let bleCommendStr = tmpble.join('')
 
        const buffer =  hexUtil.hexToArrayBuffer(bleCommendStr);
 
        
        setTimeout(() => {
             sendBleComd(commend,bleCommendStr,buffer as any,callback);
         }, 100);
 
         setTimeout(() => {
             sendBleComd(commend,bleCommendStr,buffer as any,callback);
         }, 200);

    }
}


///发送命令
export function sendCMD(commend:string,callback:any){



    if(User.isConnectedBlueTooth === false && User.isTempConnectedBlueTooth === false ){
       toast.infoToast('蓝牙已断开连接')
       return
    }

      //查询、关机和激活命令不做过期时间的判断
      if(
        commend !== 'BLE_COMMEND_SET_START_STOP_TAG_STOP' &&
        commend !== 'BLE_COMMEND_GET_RUNTIME_DATA' &&
        commend !== 'BLE_COMMEND_GET_VARIABLE_RANGE_DATA' &&
        commend !== 'BLE_COMMEND_GET_VOLTAGE_LEVEL' &&
        commend !== 'BLE_COMMEND_GET_BLE_VERSION' &&
        commend !== 'BLE_COMMEND_GET_DEVICE_DATA_STATUS' &&
        commend !== 'BLE_COMMEND_ACTIVE_EQUIPMENT' &&
        commend !== 'BLE_COMMEND_GET_UNIONID' &&
        commend !== 'BLE_COMMEND_SET_CONTROL_STATUS_MANUAL'){
            
            if(judgeCanSendCmdWhenTrailTimeIsOvered() === false){
                return;
            }
 
     }


    

     if(commend !== 'BLE_COMMEND_GET_RUNTIME_DATA' &&
        commend !== 'BLE_COMMEND_GET_VARIABLE_RANGE_DATA' &&
        commend !== 'BLE_COMMEND_GET_VOLTAGE_LEVEL' &&
        commend !== 'BLE_COMMEND_GET_BLE_VERSION' &&
        commend !== 'BLE_COMMEND_GET_DEVICE_DATA_STATUS' &&
        commend !== 'BLE_COMMEND_ACTIVE_EQUIPMENT' &&
        commend !== 'BLE_COMMEND_GET_UNIONID' &&
        commend !== 'BLE_COMMEND_SET_CONTROL_STATUS_MANUAL'){
        resetAllTempString()
    }

    let msg = getCMDStr(commend)



    // if(commend !== 'BLE_COMMEND_GET_RUNTIME_DATA'){
    //     console.log('蓝牙数据指令'+msg)

    // }


    const buffer =  hexUtil.hexToArrayBuffer(msg);
    sendBleComd(commend,msg,buffer as any,callback);

    if(commend !== 'BLE_COMMEND_GET_RUNTIME_DATA' &&
       commend !== 'BLE_COMMEND_GET_VARIABLE_RANGE_DATA' &&
       commend !== 'BLE_COMMEND_GET_VOLTAGE_LEVEL' &&
       commend !== 'BLE_COMMEND_GET_DEVICE_DATA_STATUS' &&
       commend !== 'BLE_COMMEND_GET_UNIONID'&&
       commend !== 'BLE_COMMEND_GET_CONTROL_VERSION'&&
       commend !== 'BLE_COMMEND_GET_BLE_VERSION'){

        setTimeout(() => {
            sendBleComd(commend,msg,buffer as any,callback);
        }, 100);

        setTimeout(() => {
            sendBleComd(commend,msg,buffer as any,callback);
        }, 200);
    }

}

export function sendBleComd(commend:string,msg:string,buffer:string,callback:any){
    let deviceId = User.currentConnectedBlueTooth.deviceId
    let service = User.currentConnectedBlueTooth.service
    let characteristic = User.currentConnectedBlueTooth.characteristic

    
    uni.writeBLECharacteristicValue({
      deviceId: deviceId,
      serviceId: service, 
      characteristicId: characteristic, 
      value: buffer as any,
      success(res) {
        if(commend !== 'BLE_COMMEND_GET_RUNTIME_DATA'){
            console.log('指令：'+commend+' 蓝牙数据'+msg+'发送成功')

        }
        callback(true)
      },
      fail(err) {
        console.log('蓝牙数据发送失败：'+JSON.stringify(err))
        callback(false)

      }
    })
}

//开启监听蓝牙数据回调
export function notifyBluetoothValueCallback(callback:any){
    let deviceId = User.currentConnectedBlueTooth.deviceId
    let serviceId = User.currentConnectedBlueTooth.service
    let characteristicId = User.currentConnectedBlueTooth.characteristic


    //开启回写数据的监听
    uni.notifyBLECharacteristicValueChange({
        state: true, // 启用 notify 功能
        // 这里的 deviceId 需要已经通过 createBLEConnection 与对应设备建立链接
        deviceId,
        // 这里的 serviceId 需要在 getBLEDeviceServices 接口中获取
        serviceId,
        // 这里的 characteristicId 需要在 getBLEDeviceCharacteristics 接口中获取
        characteristicId,
        success(res) {
          console.log('开启蓝牙数据回写成功')
        },
        fail(res) {
            console.log('开启蓝牙数据回写失败'+JSON.stringify(res))
          }
      })

      ///回写数据改变监听
      uni.onBLECharacteristicValueChange(function (res) {
        // console.log(`特征值： ${res.characteristicId} 改变了, 回写数据： ${res.value}`)

        let decStr = hexUtil.ab2hex(res.value).toUpperCase()
    // if(decStr.startsWith('5A01') === false){
        console.log('回写数据长度：'+decStr.length+' 十六进制：'+decStr)
    // }
        // let decStr = hexUtil.hexToString(hex).toUpperCase()
        // console.log('回写数据十进制：'+decStr)



        // let string_voltage_variable = User.bluetoothCallbackData.string_voltage_variable///所有变量范围接收字符串
        // let string_voltage_level = User.bluetoothCallbackData.string_voltage_level ///电压等级接收字符串
        // let string_runtime_data = User.bluetoothCallbackData.string_runtime_data ///实时数据接收字符串
        // let string_ble_version_data = User.bluetoothCallbackData.string_ble_version_data ///蓝牙版本号接收字符串
        // let string_ble_data_status_data = User.bluetoothCallbackData.string_ble_data_status_data///使能变量状态接收字符串

        let string_voltage_variable = ''///所有变量范围接收字符串
        let string_voltage_level = '' ///电压等级接收字符串
        let string_runtime_data = '' ///实时数据接收字符串
        let string_ble_version_data = '' ///蓝牙版本号接收字符串
        let string_ble_data_status_data = ''///使能变量状态接收字符串
        let string_control_version_data = ''///控制器版本号接收字符串


        //实时数据
        if(decStr.startsWith('5A01')){
            // console.log('判断接收到实时变量：'+decStr)
           string_runtime_data = decStr
        }
         //使能变量范围
         if(decStr.startsWith('5A0B')){
            string_ble_data_status_data = decStr
        }


         //变量范围
         if(decStr.startsWith('5A02')){
            string_voltage_variable = decStr
        }


         //电压等级
         if(decStr.startsWith('5A04')){
            string_voltage_level = decStr
        }


          //蓝牙协议唯一id
          if(decStr.startsWith('5A0C') || (decStr.length === 36 && decStr.endsWith('0D0A')) || (decStr.length === 38 && decStr.endsWith('0D0A'))){            console.log('接收到蓝牙协议id:'+decStr)
           
            // //tofix
            // decStr = '5a0c0c00370042001150315638363720980d0a'.toUpperCase()
           
            if(decStr.length === 36 && decStr.endsWith('0D0A')){
                 //旧的协议
                 User.bluetoothCallbackData.string_device_union_id_data = '-1'
            }else{
                //新的协议
                ///掐头去尾
                decStr = decStr.replace('5A0C','');
                decStr = decStr.replace('0D0A','');

                User.bluetoothCallbackData.string_device_union_id_data = decStr


            }
        }

    


          //蓝牙版本
          if(decStr.startsWith('5A06')){
                string_ble_version_data += decStr
            }

             //控制器版本号
         if(decStr.startsWith('5A05')){
            string_control_version_data= decStr
            // string_control_version_data = '5A050B004A69614C69636875616E67A90D0A'

        }

        // //不是头，不是尾   就拼接中间部分
        // if(decStr.startsWith('5A') === false && decStr.endsWith('0D0A') === false ){
        //     string_runtime_data = string_runtime_data + decStr
        //     string_voltage_variable = string_voltage_variable + decStr
        //     string_voltage_level = string_voltage_level + decStr
        //     string_ble_version_data = string_ble_version_data + decStr
        //     string_ble_data_status_data = string_ble_data_status_data + decStr

        // }

        //   //不是头，是尾   就拼接尾部
        //   if(decStr.startsWith('5A') === false && decStr.endsWith('0D0A') ){
        //     string_runtime_data = string_runtime_data + decStr
        //     string_voltage_variable = string_voltage_variable + decStr
        //     string_voltage_level = string_voltage_level + decStr
        //     string_ble_version_data = string_ble_version_data + decStr
        //     string_ble_data_status_data = string_ble_data_status_data + decStr

        // }

    

        //旧设备142位，新设备156位
        if(string_runtime_data.length == 142  || string_runtime_data.length == 156 || string_runtime_data.length == 160)
        {
            // //tofix
            // string_runtime_data = '5A01470001DA00020000030004000000000539000000060000000007000000000800000900000A000B00000C000EFF0F00110012040D33003400350036EB00370338040139F0003A003B02270D0A'

            // string_runtime_data = '5A01470001DA00020000030004000000000539000000060000000007000000000800000900000A000B00000C000EFF0F00110012040D33003400350036EB00370338040139F0003A003B023C01270D0A'
            // console.log('接收到实时数据：'+string_runtime_data)
            // User.bluetoothCallbackData.string_runtime_data = string_runtime_data
            parseBleData(string_runtime_data)




        }

        if(string_voltage_variable.length == 74)
        {
            // User.bluetoothCallbackData.string_voltage_variable = string_voltage_variable
            parseBleData(string_voltage_variable)
        }

        if(string_voltage_level.length == 46)
        {
            parseBleData(string_voltage_level)
            // User.bluetoothCallbackData.string_voltage_level = string_voltage_level
        }

        if(string_ble_data_status_data.length == 102){
            parseBleData(string_ble_data_status_data)
            // User.bluetoothCallbackData.string_ble_data_status_data = string_ble_data_status_data
        }

        if(string_ble_version_data.startsWith('5A06') && string_ble_version_data.endsWith('0D0A') ){
            parseBleData(string_ble_version_data)
            // User.bluetoothCallbackData.string_ble_version_data = string_ble_version_data
        }

        if(string_control_version_data.startsWith('5A05') && string_control_version_data.endsWith('0D0A') ){

            parseBleData(string_control_version_data)
            // User.bluetoothCallbackData.string_ble_version_data = string_ble_version_data
        }

        callback()
      })

}





  ///解析数据
  export function parseBleData(data:string){

    // console.log('开始解析数据：'+data)
    //实时数据
    if(data.startsWith('5A01')){

      parseRuntimeData(data)
    }
    //变量范围数据
    if(data.startsWith('5A02')){
        parseVariableRangeData(data)
    }
    //电压等级数据
    if(data.startsWith('5A04')){
        parseVoltageLeveldata(data)
    }
    //蓝牙版本
    if(data.startsWith('5A06')){
        parseBleVersionData(data)
    }
    //解析使能变量范围
    if(data.startsWith('5A0B')){
        parseDeviceDataStatus(data)
    }

    //解析使能变量范围
    if(data.startsWith('5A05')){
        parseControlVersionData(data)
    }

  }


  ///解析控制器版本数据
function parseControlVersionData(data:String){
    if(data.length < 6){
        return
    }

    let bleLengthHexStr = data.slice(4,6)

    let bleLengthDecStr = hexUtil.hexToString(bleLengthHexStr)

    let  bleLengthDec =  parseInt(bleLengthDecStr)

    if(bleLengthDec > 0 && data.length > (8+bleLengthDec*2)){

        let  bleVersionStr = ''
        let total =  8+bleLengthDec*2
        for(let i=8; i<total; i+=2){
            let hexStr = data.slice(i,i+2)
            let asciiStr = hexUtil.stringToASCII(hexStr)
            bleVersionStr = bleVersionStr + asciiStr
        }

        User.currentChoosedMachineBleData.control_version = bleVersionStr
        console.log('解析出来的控制器版本号:'+bleVersionStr)
    }
}

///解析蓝牙版本数据
function parseBleVersionData(data:String){
    if(data.length < 6){
        return
    }

    let bleLengthHexStr = data.slice(4,6)

    let bleLengthDecStr = hexUtil.hexToString(bleLengthHexStr)

    let  bleLengthDec =  parseInt(bleLengthDecStr)
    if(bleLengthDec > 0 && data.length > (8+bleLengthDec*2)){

        let  bleVersionStr = ''
        let total =  8+bleLengthDec*2
        for(let i=8; i<total; i+=2){
            let hexStr = data.slice(i,i+2)
            let asciiStr = hexUtil.stringToASCII(hexStr)
            bleVersionStr = bleVersionStr + asciiStr
        }

        User.currentChoosedMachineBleData.blue_tooth_version = bleVersionStr

    }
}

///电压等级数据
function parseVoltageLeveldata(data:String){
    let locate = 8
    let length = data.length - 8 - 6
    let res = data.slice(locate,length)
    let array = [] as any
    for(let i=0; i<res.length/4; i++){
        let loc =  i*4;
        let tmp = res.slice(loc, loc+4)
        array.push(tmp)

    }
    if(array.length > 0){
        let rangeArray = [] as any
        for(let i=0; i<array.length; i++){
            
             let levelHex = reverseStrByEveryTwoLetter(array[i])
             let levelDec = hexUtil.hexToString(levelHex)
             if(parseInt(levelDec) > 0){
                let float = parseFloat(levelDec) / 10.0
                let res = float.toString()
                rangeArray.push(res)
             }
        }

        User.currentChoosedMachineBleData.voltage_level_range = rangeArray

    }else{
        User.currentChoosedMachineBleData.voltage_level_range = array

    }
    console.log('电压等级范围最大值：'+JSON.stringify(User.currentChoosedMachineBleData.voltage_level_range))
    
}

///解析变量范围数据
function parseVariableRangeData(data:String){

                //取中间有效数据内容
                let res = data.slice(8,data.length - 6);
                let array = [] as any;
                for(let i=0; i<res.length/10;i++)
                {
                    let locate = i * 10
                    let length = locate + 10
                    let tmp = res.slice(locate,length)
                    array.push(tmp)
                }
    
    //发电电压 380018001a
    //自启电压 360017001d
//    #warning TODOO 假数据
//                array = [@["0100004303",
//                        "02000002d8",
//                        "0800009600",
//                        "0900000001",
//                        "360017001d",
//                        "380018001a",] mutableCopy];
                
console.log('解析出的变量范围数据:'+JSON.stringify(array))
                for(let i=0; i<array.length; i++)
                {
                    let itemData = array[i]
                    let type = getVariableRangeDataType(itemData);
                    let minValue = getVariableRangeMinValueType(itemData);
                    let maxValue = getVariableRangeMaxValueType(itemData);
                    // console.log('变量范围数据type:'+type)
                    // console.log('变量范围数据minValue:'+minValue)
                    // console.log('变量范围数据maxValue:'+maxValue)

                    maxValue = parseInt(maxValue,16).toString()
                    minValue = parseInt(minValue,16).toString()
                   
                    // console.log('minValue:'+minValue)
                    // console.log('maxValue:'+maxValue)

                    
                    //电压
                    if(type === '01')
                    {
                        User.currentChoosedMachineBleData.max_voltage = (parseInt(maxValue)/10.0).toString()
                        User.currentChoosedMachineBleData.min_voltage = minValue;
                    }
                    //功率
                    if(type === '02')
                    {
                        User.currentChoosedMachineBleData.max_power = maxValue;
                        User.currentChoosedMachineBleData.min_power = minValue;
                    }
                    //转速
                    if(type === '08')
                    {
                        User.currentChoosedMachineBleData.max_speed = maxValue;
                        User.currentChoosedMachineBleData.min_speed = minValue;
                    }
                    //油门
                    if(type === '09')
                    {
                        User.currentChoosedMachineBleData.max_gas = maxValue;
                        User.currentChoosedMachineBleData.min_gas = minValue;
                    }
                    //自启电压
                    if(type === '36')
                    {
                        User.currentChoosedMachineBleData.self_start_voltage_max =(parseInt(maxValue)/10.0).toString() ;
                        User.currentChoosedMachineBleData.self_start_voltage_min =(parseInt(minValue)/10.0).toString() ;

                    }
                    //发电电压
                    if(type === '38')
                    {
                        User.currentChoosedMachineBleData.electricity_voltage_max =(parseInt(maxValue)/10.0).toString() ; 
                        User.currentChoosedMachineBleData.electricity_voltage_min =(parseInt(minValue)/10.0).toString() ;
                        
                    }
                    
                    
                }

                // console.log('最大电压：'+User.currentChoosedMachineBleData.max_voltage);
                // console.log('最小电压：'+User.currentChoosedMachineBleData.max_voltage);
                // console.log('最大功率：'+User.currentChoosedMachineBleData.max_power);
                // console.log('最小功率：'+User.currentChoosedMachineBleData.min_power);
                // console.log('最大转速：'+User.currentChoosedMachineBleData.max_speed);
                // console.log('最小转速：'+User.currentChoosedMachineBleData.min_speed);
                // console.log('最大油门：'+User.currentChoosedMachineBleData.max_gas);
                // console.log('最小油门：'+User.currentChoosedMachineBleData.min_gas);
                // console.log('最大自启电压：'+User.currentChoosedMachineBleData.self_start_voltage_max);
                // console.log('最小自启电压：'+User.currentChoosedMachineBleData.self_start_voltage_min);
                // console.log('最大发点电压：'+User.currentChoosedMachineBleData.electricity_voltage_max);
                // console.log('最小发点电压：'+User.currentChoosedMachineBleData.electricity_voltage_min);
}


///解析使能变量范围
function parseDeviceDataStatus(data:String){
        //5A 0B
        // xx xx
        // 01 01
        // 02 01
        // 03 01
        // 04 01
        // 05 01
        // 06 01
        // 07 01
        // 08 01
        // 09 01
        // 0A 01
        // 0B 01
        // 0C 01
        // 0E 01
        // 0F 01
        // 33 01
        // 34 01
        // 35 01
        // 36 01
        // 37 01
        // 38 01
        // 39 01
        // 3A 00
        // ** 0D 0A
    
    
        let tmpvalidData = data.slice(8, data.length);
        let validData = tmpvalidData.slice(0, tmpvalidData.length-6);
        
        let dataArray = getDeviceDataStatus(validData);
        if(dataArray.length == 22){
            //01电压
            let voltage = getDataStausForm(dataArray[0]);
            //02功率
            let power = getDataStausForm(dataArray[1]);
            //03频率
            let frequency = getDataStausForm(dataArray[2]);
            //04单次运行时间
            let single_run_time = getDataStausForm(dataArray[3]);
            //05累计运行时间
            let total_run_time = getDataStausForm(dataArray[4]);
            //06单次发电功率
            let single_electricity_power = getDataStausForm(dataArray[5]);
            //07累计发电功率
            let total_electricity_power = getDataStausForm(dataArray[6]);
            //08转速
            let speed = getDataStausForm(dataArray[7]);
            //09角度（油门）
            let gas = getDataStausForm(dataArray[8]);
            //0a机油剩余量
            let oil_left = getDataStausForm(dataArray[9]);
            //0b报警状态开关量
            let warn_status_num = getDataStausForm(dataArray[10]);
            //0c发动机运行状态
            let engine_run_status = getDataStausForm(dataArray[11]);
            //0e汽油剩余量
            let gas_left = getDataStausForm(dataArray[12]);
            //0f温度
            let temperature = getDataStausForm(dataArray[13]);
            //33工作模式
            let work_mode = getDataStausForm(dataArray[14]);
            //34控制方式
            let control_way = getDataStausForm(dataArray[15]);
            //35启动标志
            let start_tag = getDataStausForm(dataArray[16]);
            //36自启电压
            let self_start_voltage = getDataStausForm(dataArray[17]);
            //37自停模式
            let self_stop_mode = getDataStausForm(dataArray[18]);
            //38发电电压
            let electricity_voltage = getDataStausForm(dataArray[19]);
            //39电压登记
            let power_level = getDataStausForm(dataArray[20]);
            //3a脚踏信号模式
            let foot_signal_model = getDataStausForm(dataArray[21]);
            
            
           User.currentChoosedMachineBleData.is_commend_hidden_voltage = parseInt(voltage) == 0;
           User.currentChoosedMachineBleData.is_commend_hidden_power = parseInt(power) == 0;
           User.currentChoosedMachineBleData.is_commend_hidden_frequency = parseInt(frequency) == 0;
           User.currentChoosedMachineBleData.is_commend_hidden_single_run_time = parseInt(single_run_time) == 0;
           User.currentChoosedMachineBleData.is_commend_hidden_total_run_time = parseInt(total_run_time) == 0;
           User.currentChoosedMachineBleData.is_commend_hidden_single_electricity_power = parseInt(single_electricity_power) == 0;
           User.currentChoosedMachineBleData.is_commend_hidden_total_electricity_power = parseInt(total_electricity_power) == 0;
           User.currentChoosedMachineBleData.is_commend_hidden_speed = parseInt(speed) == 0;
           User.currentChoosedMachineBleData.is_commend_hidden_gas = parseInt(gas) == 0;
           User.currentChoosedMachineBleData.is_commend_hidden_oil_left = parseInt(oil_left) == 0;
           User.currentChoosedMachineBleData.is_commend_hidden_warn_status_num = parseInt(warn_status_num) == 0;
           User.currentChoosedMachineBleData.is_commend_hidden_engine_run_status = parseInt(engine_run_status) == 0;
           User.currentChoosedMachineBleData.is_commend_hidden_gas_left = parseInt(gas_left) == 0;
           User.currentChoosedMachineBleData.is_commend_hidden_temperature = parseInt(temperature) == 0;
           User.currentChoosedMachineBleData.is_commend_hidden_work_mode = parseInt(work_mode) == 0;
           User.currentChoosedMachineBleData.is_commend_hidden_control_way = parseInt(control_way) == 0;
           User.currentChoosedMachineBleData.is_commend_hidden_start_tag = parseInt(start_tag) == 0;
           User.currentChoosedMachineBleData.is_commend_hidden_self_start_voltage = parseInt(self_start_voltage) == 0;
           User.currentChoosedMachineBleData.is_commend_hidden_self_stop_mode = parseInt(self_stop_mode) == 0;
           User.currentChoosedMachineBleData.is_commend_hidden_electricity_voltage = parseInt(electricity_voltage) == 0;
           User.currentChoosedMachineBleData.is_commend_show_power_level = parseInt(power_level) === 1;
           User.currentChoosedMachineBleData.is_commend_show_foot_signal_model = parseInt(foot_signal_model) == 1;
            
           console.log('是否隐藏变量-----------------------start')
           console.log("01电压："+voltage);
           console.log("02功率："+power);
           console.log("03频率："+frequency);
           console.log("04单次运行时间："+single_run_time);
           console.log("05累计运行时间："+total_run_time);
           console.log("06单次发电功率："+single_electricity_power);
           console.log("07累计发电功率："+total_electricity_power);
           console.log("08转速："+speed);
           console.log("09油门："+gas);
           console.log("0a机油剩余量："+oil_left);
           console.log("0b报警状态开关量："+warn_status_num);
           console.log("0c发动机运行状态："+engine_run_status);
           console.log("0e汽油剩余量："+gas_left);
           console.log("0f温度："+temperature);
           console.log("33工作模式："+work_mode);
           console.log("34控制方式："+control_way);
           console.log("35启动标志："+start_tag);
           console.log("36自启电压："+self_start_voltage);
           console.log("37自停模式："+self_stop_mode);
           console.log("38发电电压："+electricity_voltage);
           console.log("39电压等级："+power_level);
           console.log("3a脚踏信号模式："+foot_signal_model);
           console.log('是否隐藏变量-----------------------end')
           console.log('脚踏信号---：'+User.currentChoosedMachineBleData.is_commend_show_foot_signal_model )

        //    console.log("\n\n\n");

        }
}

///解析实时数据
function parseRuntimeData(data:String){
    User.isReceivedRuntimeData = true

    console.log('实时数据的长度：'+data.length)
    if(data.length == 156){ //激活试用时间
        User.currentChoosedMachineBleData.is_lng = false;
        //新设备
        parseNewVersionEquipmentRuntimeData(data);
    }else if(data.length == 160){ //燃料类型
        User.currentChoosedMachineBleData.is_lng = true;
        //新设备
        parseNewVersion3EquipmentRuntimeData(data);
    }else{
        User.currentChoosedMachineBleData.is_lng = false;

        //旧设备
        parseOldVersionEquipmentRuntimeData(data);
    }

    ///超过20条就上报一次
    if(User.powerList.length > 20){
        btViewmodel.uploadDeviceDataReq()
    }

}

///解析旧设备的实时数据
function parseOldVersionEquipmentRuntimeData(data:String){
    // console.log('这里开始解析老版本的数据了：'+data)
        //01电压    10-14
        let voltage = data.slice(10, 14);
        //02功率     16-20
        let power = data.slice(16, 20);
        //03频率     22-24
        let frequency = data.slice(22, 24);
        //04单次运行时间 26-34
        let single_run_time = data.slice(26, 34);
        //05累计运行时间 36-44
        let total_run_time = data.slice(36, 44);
        //06单次发电功率 46-54
        let single_electricity_power = data.slice(46, 54);
        //07累计发电功率 56-64
        let total_electricity_power = data.slice(56, 64);
        //08转速 66-70
        let speed = data.slice(66, 70);
        //09角度（油门） 72-76
        let gas = data.slice(72, 76);
        //0a机油剩余量 78-80
        let oil_left = data.slice(78, 80);
        //0b报警状态开关量 82-86
        let warn_status_num = data.slice(82, 86);
        //0c发动机运行状态 88-90
        let engine_run_status = data.slice(88, 90);
        //0e油量 92-94
        let oil_count = data.slice(92, 94);
        //0f温度 96-98
        let temperature = data.slice(96, 98);
        //33工作模式 100-102
        let work_mode = data.slice(100, 102);
        //34控制方式 104-106
        let control_way = data.slice(104, 106);
        //35启动标志 108-110
        let start_tag = data.slice(108, 110);
        //36自启电压 112-116
        let self_start_voltage = data.slice(112, 116);
        //37自停模式 118-120
        let self_stop_mode = data.slice(118, 120);
        //38发电电压 122-126
        let electricity_voltage = data.slice(122, 126);
        //39电压等级 128-132
        let electricity_level = data.slice(128, 132);
        //3a脚踏信号模式 134-136
        let foot_signal_model = data.slice(134, 136);
        
    //    console.log("01电压："+voltage);
    //    console.log("02功率："+power);
    //    console.log("03频率："+frequency);
    //    console.log("04单次运行时间："+single_run_time);
    //    console.log("05累计运行时间："+total_run_time);
    //    console.log("06单次发电功率："+single_electricity_power);
    //    console.log("07累计发电功率："+total_electricity_power);
    //    console.log("08转速："+speed);
    //    console.log("09油门："+gas);
    //    console.log("0a机油剩余量："+oil_left);
    //    console.log("0b报警状态开关量："+warn_status_num);
    //    console.log("0c发动机运行状态："+engine_run_status);
    //    console.log("0e油量："+oil_count);
    //    console.log("0f温度："+temperature);
    //    console.log("33工作模式："+work_mode);
    //    console.log("34控制方式："+control_way);
    //    console.log("35启动标志："+start_tag);
    //    console.log("36自启电压："+self_start_voltage);
    //    console.log("37自停模式："+self_stop_mode);
    //    console.log("38发电电压："+electricity_voltage);
    //    console.log("39电压等级："+electricity_level);
    //    console.log("3a脚踏信号模式："+foot_signal_model);
    //    console.log("\n\n\n");
        
        voltage = reverseStrByEveryTwoLetter(voltage);
        power = reverseStrByEveryTwoLetter(power);
        frequency = reverseStrByEveryTwoLetter(frequency);
        single_run_time = reverseStrByEveryTwoLetter(single_run_time);
        total_run_time = reverseStrByEveryTwoLetter(total_run_time);
        single_electricity_power = reverseStrByEveryTwoLetter(single_electricity_power);
        total_electricity_power = reverseStrByEveryTwoLetter(total_electricity_power);
        speed = reverseStrByEveryTwoLetter(speed);
        gas = reverseStrByEveryTwoLetter(gas);
        oil_left = reverseStrByEveryTwoLetter(oil_left);
        warn_status_num = reverseStrByEveryTwoLetter(warn_status_num);
        engine_run_status = reverseStrByEveryTwoLetter(engine_run_status);
        oil_count = reverseStrByEveryTwoLetter(oil_count);
        temperature = reverseStrByEveryTwoLetter(temperature);
        work_mode = reverseStrByEveryTwoLetter(work_mode);
        control_way = reverseStrByEveryTwoLetter(control_way);
        start_tag = reverseStrByEveryTwoLetter(start_tag);
        self_start_voltage = reverseStrByEveryTwoLetter(self_start_voltage);
        self_stop_mode = reverseStrByEveryTwoLetter(self_stop_mode);
        electricity_voltage = reverseStrByEveryTwoLetter(electricity_voltage);
        electricity_level = reverseStrByEveryTwoLetter(electricity_level);
        foot_signal_model = reverseStrByEveryTwoLetter(foot_signal_model);
        
    //    console.log("反转-01电压："+voltage);
    //    console.log("反转-02功率："+power);
    //    console.log("反转-03频率："+frequency);
    //    console.log("反转-04单次运行时间："+single_run_time);
    //    console.log("反转-05累计运行时间："+total_run_time);
    //    console.log("反转-06单次发电功率："+single_electricity_power);
    //    console.log("反转-07累计发电功率："+total_electricity_power);
    //    console.log("反转-08转速："+speed);
    //    console.log("反转-09油门："+gas);
    //    console.log("反转-0a机油剩余量："+oil_left);
    //    console.log("反转-0b报警状态开关量："+warn_status_num);
    //    console.log("反转-0c发动机运行状态："+engine_run_status);
    //    console.log("反转-0e油量："+oil_count);
    //    console.log("反转-0f温度："+temperature);
    //    console.log("反转-33工作模式："+work_mode);
    //    console.log("反转-34控制方式："+control_way);
    //    console.log("反转-35启动标志："+start_tag);
    //    console.log("反转-36自启电压："+self_start_voltage);
    //    console.log("反转-37自停模式："+self_stop_mode);
    //    console.log("反转-38发电电压："+electricity_voltage);
    //    console.log("反转-39电压等级："+electricity_level);
    //    console.log("反转-3a脚踏信号模式："+foot_signal_model);
    //    console.log("\n\n\n");
    //
        
        User.currentChoosedMachineBleData.is_new_version_equipment = false;
        User.currentChoosedMachineBleData.frequency = hexUtil.hexToString(frequency);
        User.currentChoosedMachineBleData.single_run_time = hexUtil.hexToString(single_run_time);
        User.currentChoosedMachineBleData.total_run_time = hexUtil.hexToString(total_run_time);
        User.currentChoosedMachineBleData.single_elec_power =  (parseInt(single_electricity_power,16)/10.0).toString()
        User.currentChoosedMachineBleData.total_power_power = (parseInt(total_electricity_power,16)/10.0).toString()
        User.currentChoosedMachineBleData.oil_mass = hexUtil.hexToString(oil_left);
        let warnStatusStr =  hexUtil.hexToString(warn_status_num);
        if( User.currentChoosedMachineBleData.warn_status_tips !== warnStatusStr){
            User.currentConnectedEquipment.warning_tips = ''
          }
        User.currentChoosedMachineBleData.warn_status_tips = warnStatusStr;
        User.currentChoosedMachineBleData.engine_run_status = hexUtil.hexToString(engine_run_status);
        User.currentChoosedMachineBleData.work_mode = hexUtil.hexToString(work_mode);
        User.currentChoosedMachineBleData.control_status = hexUtil.hexToString(control_way);
        User.currentChoosedMachineBleData.start_stop_tag = hexUtil.hexToString(start_tag);
        User.currentChoosedMachineBleData.self_start_voltage = (parseInt(self_start_voltage,16)/10.0).toString()
        User.currentChoosedMachineBleData.self_stop_mode = hexUtil.hexToString(self_stop_mode);
        User.currentChoosedMachineBleData.electricity_voltage = (parseInt(electricity_voltage,16)/10.0).toString()
        User.currentChoosedMachineBleData.pedal_signal_model = hexUtil.hexToString(foot_signal_model);
    //    User.currentChoosedMachineBleData.curr_oil_count = hexUtil.hexToString(oil_count);
        User.currentChoosedMachineBleData.curr_temperature = hexUtil.hexToString(temperature);
        User.currentChoosedMachineBleData.curr_voltage_level = (parseInt(electricity_level,16)/10.0).toString()
        
        if(parseInt(User.currentChoosedMachineBleData.engine_run_status) == 1 && parseInt(User.currentChoosedMachineBleData.control_status) == 1 &&  parseInt(User.currentChoosedMachineBleData.start_stop_tag) == 1){
            User.currentChoosedMachineBleData.is_running = true;
        }else{
            User.currentChoosedMachineBleData.is_running = false;
        }
        
        
        let voltageModel = {} as any;
        voltageModel.type = 100
        voltageModel.timestamp = Math.floor(new Date().getTime() / 1000);
        voltageModel.data =  (parseInt(voltage,16)/10.0).toString()
        voltageModel.deviceID = User.currentConnectedBlueTooth.deviceId
               
        let speedModel = {} as any;
        speedModel.timestamp = Math.floor(new Date().getTime() / 1000);
        speedModel.type = 101
        speedModel.data = hexUtil.hexToString(speed);
        speedModel.deviceID = User.currentConnectedBlueTooth.deviceId
               
        let powerModel = {} as any;
        powerModel.timestamp = Math.floor(new Date().getTime() / 1000);
        powerModel.type = 102
        powerModel.data = hexUtil.hexToString(power);
        powerModel.deviceID = User.currentConnectedBlueTooth.deviceId
               
        let gasModel = {} as any
        gasModel.timestamp = Math.floor(new Date().getTime() / 1000);
        gasModel.type = 103
        gasModel.data = hexUtil.hexToString(gas);
        gasModel.deviceID = User.currentConnectedBlueTooth.deviceId
        
        let oilCountModel = {} as any;
        oilCountModel.timestamp = Math.floor(new Date().getTime() / 1000);
        oilCountModel.type = 104
        oilCountModel.data = hexUtil.hexToString(oil_count);
        oilCountModel.deviceID = User.currentConnectedBlueTooth.deviceId


        let runtimeModel = {} as any;
        runtimeModel.timestamp = Math.floor(new Date().getTime() / 1000);
        runtimeModel.type = 104
        runtimeModel.totalUseTime = User.currentChoosedMachineBleData.total_run_time;
        runtimeModel.singleUseTime = User.currentChoosedMachineBleData.single_run_time;
        runtimeModel.totalElc = User.currentChoosedMachineBleData.total_power_power;
        runtimeModel.singleElc = User.currentChoosedMachineBleData.single_elec_power;
        runtimeModel.deviceID = User.currentConnectedBlueTooth.deviceId
    
        if(User.voltageList === null){
            User.voltageList = [] as any
        }
    
        if(User.powerList === null){
            User.powerList = [] as any
        }
    
        if(User.acceleratorList === null){
            User.acceleratorList = [] as any
        }
    
        if(User.speedList === null){
            User.speedList = [] as any
        }
    
        if(User.oilCountList === null){
            User.oilCountList = [] as any
        }
    
        if(User.runtimeList === null){
            User.runtimeList = [] as any
        }
    
        User.voltageList.push(voltageModel)
        User.powerList.push(powerModel)
        User.acceleratorList.push(gasModel)
        User.speedList.push(speedModel)
        User.oilCountList.push(oilCountModel)
        User.runtimeList.push(runtimeModel)
        
    //#warning TODOO 临时测试
    //    User.currentChoosedMachineBleData.engine_run_status = kRandomNum(2);
    //    User.currentChoosedMachineBleData.work_mode = kRandomNum(4);
    //    User.currentChoosedMachineBleData.curr_voltage_level = kRandomNum(30);
    //    User.currentChoosedMachineBleData.control_status = kRandomNum(2);
    //    User.currentChoosedMachineBleData.self_start_voltage = [NSString stringWithFormat:"%.0f",kRandomNum(100).floatValue/10.);
    //    User.currentChoosedMachineBleData.self_stop_mode = kRandomNum(10);
    //    User.currentChoosedMachineBleData.electricity_voltage = [NSString stringWithFormat:"%.0f",kRandomNum(100).floatValue/10.);
    //    User.currentChoosedMachineBleData.pedal_signal_model = kRandomNum(3);
    //    voltageModel.data =  kRandomNum(83);
    //    powerModel.data =  kRandomNum(55298);
    //    gasModel.data =  kRandomNum(150);
    //    oilCountModel.data =  kRandomNum(100);
    //    speedModel.data =  kRandomNum(150);
    //    User.currentChoosedMachineBleData.single_run_time = Math.floor(new Date().getTime() / 1000);
    //    User.currentChoosedMachineBleData.total_run_time = Math.floor(new Date().getTime() / 1000);
    //    User.currentChoosedMachineBleData.single_elec_power = [NSString stringWithFormat:"%.1f",kRandomNum(100000).floatValue/10.);
    //    User.currentChoosedMachineBleData.total_power_power = [NSString stringWithFormat:"%.1f",kRandomNum(100000).floatValue/10.);
    //
        
        
    
        // [EquipmentCacheDataManager addVoltageData:voltageModel);
        // [EquipmentCacheDataManager addPowerData:powerModel);
        // [EquipmentCacheDataManager addAcceleratorData:gasModel);
        // [EquipmentCacheDataManager addSpeedData:speedModel);
        // [EquipmentCacheDataManager addOilCountData:oilCountModel);

     
    
        
        User.currentChoosedMachineBleData.curr_voltage = voltageModel.data;
        User.currentChoosedMachineBleData.curr_power = powerModel.data;
        User.currentChoosedMachineBleData.curr_speed = speedModel.data;
        User.currentChoosedMachineBleData.curr_gas = gasModel.data;
        User.currentChoosedMachineBleData.curr_oil_count = oilCountModel.data;
        console.log('1.0设备')
       console.log("数据-01电压："+voltageModel.data);
       console.log("数据-02功率："+powerModel.data);
       console.log("数据-03频率："+User.currentChoosedMachineBleData.frequency);
       console.log("数据-04单次运行时间："+User.currentChoosedMachineBleData.single_run_time);
       console.log("数据-05累计运行时间："+User.currentChoosedMachineBleData.total_run_time);
       console.log("数据-06单次发电功率："+User.currentChoosedMachineBleData.single_elec_power);
       console.log("数据-07累计发电功率："+User.currentChoosedMachineBleData.total_power_power);
       console.log("数据-08转速："+speedModel.data);
       console.log("数据-09油门："+gasModel.data);
       console.log("数据-0a机油剩余量："+User.currentChoosedMachineBleData.oil_mass);
       console.log("数据-0b报警状态开关量："+User.currentChoosedMachineBleData.warn_status_tips);
       console.log("数据-0c发动机运行状态："+User.currentChoosedMachineBleData.engine_run_status);
       console.log("数据-0e油量："+User.currentChoosedMachineBleData.curr_oil_count);
       console.log("数据-0f温度："+User.currentChoosedMachineBleData.curr_temperature);
       console.log("数据-33工作模式："+User.currentChoosedMachineBleData.work_mode);
       console.log("数据-34控制方式："+User.currentChoosedMachineBleData.control_status);
       console.log("数据-35启动标志："+User.currentChoosedMachineBleData.start_stop_tag);
       console.log("数据-36自启电压："+User.currentChoosedMachineBleData.self_start_voltage);
       console.log("数据-37自停模式："+User.currentChoosedMachineBleData.self_stop_mode);
       console.log("数据-38发电电压："+User.currentChoosedMachineBleData.electricity_voltage);
       console.log("数据-3a脚踏信号模式："+User.currentChoosedMachineBleData.pedal_signal_model);
    console.log("数据-39电压等级："+(parseFloat(User.currentChoosedMachineBleData.curr_voltage_level)/10.0).toFixed(0))

       console.log("\n\n\n");
        
}


///解析新设备的实时数据 2.0协议
function parseNewVersionEquipmentRuntimeData(data:String){
    // console.log('这里开始解析新版本的数据了：'+data)

    //01电压    10-14
    let voltage = data.slice(10, 14);
    //02功率     16-20
    let power = data.slice(16, 20);
    //03频率     22-24
    let frequency = data.slice(22, 24);
    //04单次运行时间 26-34
    let single_run_time = data.slice(26, 34);
    //05累计运行时间 36-44
    let total_run_time = data.slice(36, 44);
    //06单次发电功率 46-54
    let single_electricity_power = data.slice(46, 54);
    //07累计发电功率 56-64
    let total_electricity_power = data.slice(56, 64);
    //08转速 66-70
    let speed = data.slice(66, 70);
    //09角度（油门） 72-76
    let gas = data.slice(72, 76);
    //0a机油剩余量 78-80
    let oil_left = data.slice(78, 80);
    //0b报警状态开关量 82-86
    let warn_status_num = data.slice(82, 86);
    //0c发动机运行状态 88-90
    let engine_run_status = data.slice(88, 90);
    //0e油量 92-94
    let oil_count = data.slice(92, 94);
    //0f温度 96-98
    let temperature = data.slice(96, 98);
    //12试用倒计时计数
    let trail_time = data.slice(104, 108);
    //33工作模式 100-102
    let work_mode = data.slice(110, 112);
    //34控制方式 104-106
    let control_way = data.slice(114, 116);
    //35启动标志 108-110
    let start_tag = data.slice(118, 120);
    //36自启电压 112-116
    let self_start_voltage = data.slice(122, 126);
    //37自停模式 118-120
    let self_stop_mode = data.slice(128, 130);
    //38发电电压 122-126
    let electricity_voltage = data.slice(132, 136);
    //39电压等级 128-132
    let electricity_level = data.slice(138, 142);
    //3a脚踏信号模式 134-136
    let foot_signal_model = data.slice(144, 146);
    //3b激活状态
    let active_statue = data.slice(148, 150);
    
//    console.log("01电压："+voltage);
//    console.log("02功率："+power);
//    console.log("03频率："+frequency);
//    console.log("04单次运行时间："+single_run_time);
//    console.log("05累计运行时间："+total_run_time);
//    console.log("06单次发电功率："+single_electricity_power);
//    console.log("07累计发电功率："+total_electricity_power);
//    console.log("08转速："+speed);
//    console.log("09油门："+gas);
//    console.log("0a机油剩余量："+oil_left);
//    console.log("0b报警状态开关量："+warn_status_num);
//    console.log("0c发动机运行状态："+engine_run_status);
//    console.log("0e油量："+oil_count);
//    console.log("0f温度："+temperature);
//    console.log("12倒计时："+trail_time);
//    console.log("33工作模式："+work_mode);
//    console.log("34控制方式："+control_way);
//    console.log("35启动标志："+start_tag);
//    console.log("36自启电压："+self_start_voltage);
//    console.log("37自停模式："+self_stop_mode);
//    console.log("38发电电压："+electricity_voltage);
//    console.log("39电压等级："+electricity_level);
//    console.log("3a脚踏信号模式："+foot_signal_model);
//    console.log("3b激活状态："+active_statue);
//    console.log("\n\n\n");
//
    voltage = reverseStrByEveryTwoLetter(voltage);
    power = reverseStrByEveryTwoLetter(power);
    frequency = reverseStrByEveryTwoLetter(frequency);
    single_run_time = reverseStrByEveryTwoLetter(single_run_time);
    total_run_time = reverseStrByEveryTwoLetter(total_run_time);
    single_electricity_power = reverseStrByEveryTwoLetter(single_electricity_power);
    total_electricity_power = reverseStrByEveryTwoLetter(total_electricity_power);
    speed = reverseStrByEveryTwoLetter(speed);
    gas = reverseStrByEveryTwoLetter(gas);
    oil_left = reverseStrByEveryTwoLetter(oil_left);
    warn_status_num = reverseStrByEveryTwoLetter(warn_status_num);
    engine_run_status = reverseStrByEveryTwoLetter(engine_run_status);
    oil_count = reverseStrByEveryTwoLetter(oil_count);
    temperature = reverseStrByEveryTwoLetter(temperature);
    trail_time = reverseStrByEveryTwoLetter(trail_time);
    work_mode = reverseStrByEveryTwoLetter(work_mode);
    control_way = reverseStrByEveryTwoLetter(control_way);
    start_tag = reverseStrByEveryTwoLetter(start_tag);
    self_start_voltage = reverseStrByEveryTwoLetter(self_start_voltage);
    self_stop_mode = reverseStrByEveryTwoLetter(self_stop_mode);
    electricity_voltage = reverseStrByEveryTwoLetter(electricity_voltage);
    electricity_level = reverseStrByEveryTwoLetter(electricity_level);
    foot_signal_model = reverseStrByEveryTwoLetter(foot_signal_model);
    active_statue = reverseStrByEveryTwoLetter(active_statue);
//    console.log("反转-01电压："+voltage);
//    console.log("反转-02功率："+power);
//    console.log("反转-03频率："+frequency);
//    console.log("反转-04单次运行时间："+single_run_time);
//    console.log("反转-05累计运行时间："+total_run_time);
//    console.log("反转-06单次发电功率："+single_electricity_power);
//    console.log("反转-07累计发电功率："+total_electricity_power);
//    console.log("反转-08转速："+speed);
//    console.log("反转-09油门："+gas);
//    console.log("反转-0a机油剩余量："+oil_left);
//    console.log("反转-0b报警状态开关量："+warn_status_num);
//    console.log("反转-0c发动机运行状态："+engine_run_status);
//    console.log("反转-0e油量："+oil_count);
//    console.log("反转-0f温度："+temperature);
//    console.log("反转-12试用倒计时："+trail_time);
//    console.log("反转-33工作模式："+work_mode);
//    console.log("反转-34控制方式："+control_way);
//    console.log("反转-35启动标志："+start_tag);
//    console.log("反转-36自启电压："+self_start_voltage);
//    console.log("反转-37自停模式："+self_stop_mode);
//    console.log("反转-38发电电压："+electricity_voltage);
//    console.log("反转-39电压等级："+electricity_level);
//    console.log("反转-3a脚踏信号模式："+foot_signal_model);
//    console.log("反转-3b激活状态："+active_statue);
//    console.log("\n\n\n");
//
    
    User.currentChoosedMachineBleData.is_new_version_equipment = true;
        //3b 位非00全是激活状态
        if(active_statue === "00"){
            User.currentChoosedMachineBleData.is_active = "0";
        }else{
            User.currentChoosedMachineBleData.is_active = "1";
        }
    
    User.currentChoosedMachineBleData.trial_time = parseInt(hexUtil.hexToString(trail_time));
    User.currentChoosedMachineBleData.frequency = hexUtil.hexToString(frequency);
    User.currentChoosedMachineBleData.single_run_time = hexUtil.hexToString(single_run_time);
    User.currentChoosedMachineBleData.total_run_time = hexUtil.hexToString(total_run_time);
    User.currentChoosedMachineBleData.single_elec_power =  (parseInt(single_electricity_power,16)/10.0).toString()
    User.currentChoosedMachineBleData.total_power_power =  (parseInt(total_electricity_power,16)/10.0).toString()
    User.currentChoosedMachineBleData.oil_mass = hexUtil.hexToString(oil_left);


    let warnStatusStr =  hexUtil.hexToString(warn_status_num);
    if( User.currentChoosedMachineBleData.warn_status_tips !== warnStatusStr){
        User.currentConnectedEquipment.warning_tips = ''
      }
    User.currentChoosedMachineBleData.warn_status_tips = warnStatusStr;
    User.currentChoosedMachineBleData.engine_run_status = hexUtil.hexToString(engine_run_status);
    User.currentChoosedMachineBleData.work_mode = hexUtil.hexToString(work_mode);
    User.currentChoosedMachineBleData.control_status = hexUtil.hexToString(control_way);
    User.currentChoosedMachineBleData.start_stop_tag = hexUtil.hexToString(start_tag);
    User.currentChoosedMachineBleData.self_start_voltage =  (parseInt(self_start_voltage,16)/10.0).toString()
    User.currentChoosedMachineBleData.self_stop_mode = hexUtil.hexToString(self_stop_mode);
    User.currentChoosedMachineBleData.electricity_voltage =  (parseInt(electricity_voltage,16)/10.0).toString()
    User.currentChoosedMachineBleData.pedal_signal_model = hexUtil.hexToString(foot_signal_model);
   User.currentChoosedMachineBleData.curr_oil_count = hexUtil.hexToString(oil_count);
    User.currentChoosedMachineBleData.curr_temperature = hexUtil.hexToString(temperature);
    User.currentChoosedMachineBleData.curr_voltage_level =  (parseInt(electricity_level,16)/10.0).toString()
    
    if(parseInt(User.currentChoosedMachineBleData.engine_run_status) == 1 && parseInt(User.currentChoosedMachineBleData.control_status) == 1 &&  parseInt(User.currentChoosedMachineBleData.start_stop_tag) == 1){
        User.currentChoosedMachineBleData.is_running = true;
    }else{
        User.currentChoosedMachineBleData.is_running = false;
    }

    // User.currentChoosedMachineBleData.trial_time = 0;
    // User.currentChoosedMachineBleData.engine_run_status = '1';

    
    
    let voltageModel = {} as any;
    voltageModel.type = 100
    voltageModel.timestamp = Math.floor(new Date().getTime() / 1000);
    voltageModel.data =  (parseInt(voltage,16)/10.0).toString();
    voltageModel.deviceID = User.currentConnectedBlueTooth.deviceId
           
    let speedModel = {} as any;
    speedModel.timestamp = Math.floor(new Date().getTime() / 1000);
    speedModel.type = 101
    speedModel.data = hexUtil.hexToString(speed);
    speedModel.deviceID = User.currentConnectedBlueTooth.deviceId
           
    let powerModel = {} as any;
    powerModel.timestamp = Math.floor(new Date().getTime() / 1000);
    powerModel.type = 102
    powerModel.data = hexUtil.hexToString(power);
    powerModel.deviceID = User.currentConnectedBlueTooth.deviceId
           
    let gasModel = {} as any;
    gasModel.timestamp = Math.floor(new Date().getTime() / 1000);
    gasModel.type = 103
    gasModel.data = hexUtil.hexToString(gas);
    gasModel.deviceID = User.currentConnectedBlueTooth.deviceId
    
    let oilCountModel = {} as any;
    oilCountModel.timestamp = Math.floor(new Date().getTime() / 1000);
    oilCountModel.type = 104
    oilCountModel.data = hexUtil.hexToString(oil_count);
    oilCountModel.deviceID = User.currentConnectedBlueTooth.deviceId

    let runtimeModel = {} as any;
    runtimeModel.timestamp = Math.floor(new Date().getTime() / 1000);
    runtimeModel.type = 104
    runtimeModel.totalUseTime = User.currentChoosedMachineBleData.total_run_time;
    runtimeModel.singleUseTime = User.currentChoosedMachineBleData.single_run_time;
    runtimeModel.totalElc = User.currentChoosedMachineBleData.total_power_power;
    runtimeModel.singleElc = User.currentChoosedMachineBleData.single_elec_power;
    runtimeModel.deviceID = User.currentConnectedBlueTooth.deviceId

    if(User.voltageList === null){
        User.voltageList = [] as any
    }

    if(User.powerList === null){
        User.powerList = [] as any
    }

    if(User.acceleratorList === null){
        User.acceleratorList = [] as any
    }

    if(User.speedList === null){
        User.speedList = [] as any
    }

    if(User.oilCountList === null){
        User.oilCountList = [] as any
    }

    if(User.runtimeList === null){
        User.runtimeList = [] as any
    }

    User.voltageList.push(voltageModel)
    User.powerList.push(powerModel)
    User.acceleratorList.push(gasModel)
    User.speedList.push(speedModel)
    User.oilCountList.push(oilCountModel)
    User.runtimeList.push(runtimeModel)

    
//#warning TODOO 临时测试
//    User.currentChoosedMachineBleData.engine_run_status = kRandomNum(2);
//    User.currentChoosedMachineBleData.work_mode = Math.floor(Math.random()%4) + '';
//    User.currentChoosedMachineBleData.curr_voltage_level = kRandomNum(30);
//    User.currentChoosedMachineBleData.control_status = kRandomNum(2);
//    User.currentChoosedMachineBleData.self_start_voltage = [NSString stringWithFormat:"%.0f",kRandomNum(100).floatValue/10.);
//    User.currentChoosedMachineBleData.self_stop_mode = kRandomNum(10);
//    User.currentChoosedMachineBleData.electricity_voltage = [NSString stringWithFormat:"%.0f",kRandomNum(100).floatValue/10.);
//    User.currentChoosedMachineBleData.pedal_signal_model = kRandomNum(3);
//    voltageModel.data =  kRandomNum(83);
//    powerModel.data =  kRandomNum(55298);
//    gasModel.data =  kRandomNum(150);
//    oilCountModel.data =  kRandomNum(100);
//    speedModel.data =  kRandomNum(150);
//    User.currentChoosedMachineBleData.single_run_time = Math.floor(new Date().getTime() / 1000);
//    User.currentChoosedMachineBleData.total_run_time = Math.floor(new Date().getTime() / 1000);
//    User.currentChoosedMachineBleData.single_elec_power = [NSString stringWithFormat:"%.1f",kRandomNum(100000).floatValue/10.);
//    User.currentChoosedMachineBleData.total_power_power = [NSString stringWithFormat:"%.1f",kRandomNum(100000).floatValue/10.);
//
    
    
    // [EquipmentCacheDataManager addVoltageData:voltageModel);
    // [EquipmentCacheDataManager addPowerData:powerModel);
    // [EquipmentCacheDataManager addAcceleratorData:gasModel);
    // [EquipmentCacheDataManager addSpeedData:speedModel);
    // [EquipmentCacheDataManager addOilCountData:oilCountModel);

    
    User.currentChoosedMachineBleData.curr_voltage = voltageModel.data;
    User.currentChoosedMachineBleData.curr_power = powerModel.data;
    User.currentChoosedMachineBleData.curr_speed = speedModel.data;
    User.currentChoosedMachineBleData.curr_gas = gasModel.data;
    User.currentChoosedMachineBleData.curr_oil_count = oilCountModel.data;
    console.log('2.0设备')
   console.log("数据-01电压："+voltageModel.data);
   console.log("数据-02功率："+powerModel.data);
   console.log("数据-03频率："+User.currentChoosedMachineBleData.frequency);
   console.log("数据-04单次运行时间："+User.currentChoosedMachineBleData.single_run_time);
   console.log("数据-05累计运行时间："+User.currentChoosedMachineBleData.total_run_time);
   console.log("数据-06单次发电功率："+User.currentChoosedMachineBleData.single_elec_power);
   console.log("数据-07累计发电功率："+User.currentChoosedMachineBleData.total_power_power);
   console.log("数据-08转速："+speedModel.data);
   console.log("数据-09油门："+gasModel.data);
   console.log("数据-0a机油剩余量："+User.currentChoosedMachineBleData.oil_mass);
   console.log("数据-0b报警状态开关量："+User.currentChoosedMachineBleData.warn_status_tips);
   console.log("数据-0c发动机运行状态："+User.currentChoosedMachineBleData.engine_run_status);
   console.log("数据-0e油量："+User.currentChoosedMachineBleData.curr_oil_count);
   console.log("数据-0f温度："+User.currentChoosedMachineBleData.curr_temperature);
   console.log("数据-12试用倒计时："+User.currentChoosedMachineBleData.trial_time);
   console.log("数据-33工作模式："+User.currentChoosedMachineBleData.work_mode);
   console.log("数据-34控制方式："+User.currentChoosedMachineBleData.control_status);
   console.log("数据-35启动标志："+User.currentChoosedMachineBleData.start_stop_tag);
   console.log("数据-36自启电压："+User.currentChoosedMachineBleData.self_start_voltage);
   console.log("数据-37自停模式："+User.currentChoosedMachineBleData.self_stop_mode);
   console.log("数据-38发电电压："+User.currentChoosedMachineBleData.electricity_voltage);
   console.log("数据-3a脚踏信号模式："+User.currentChoosedMachineBleData.pedal_signal_model);
   console.log("数据-39电压等级："+(parseInt(User.currentChoosedMachineBleData.curr_voltage_level,16)/10.0).toString())
   console.log("数据-3b激活状态："+User.currentChoosedMachineBleData.is_active);
   console.log("\n\n\n");
    
 
}


///解析旧设备的实时数据
function parseNewVersion3EquipmentRuntimeData(data:String){
    // console.log('这里开始解析新版本的数据了：'+data)

    //01电压    10-14
    let voltage = data.slice(10, 14);
    //02功率     16-20
    let power = data.slice(16, 20);
    //03频率     22-24
    let frequency = data.slice(22, 24);
    //04单次运行时间 26-34
    let single_run_time = data.slice(26, 34);
    //05累计运行时间 36-44
    let total_run_time = data.slice(36, 44);
    //06单次发电功率 46-54
    let single_electricity_power = data.slice(46, 54);
    //07累计发电功率 56-64
    let total_electricity_power = data.slice(56, 64);
    //08转速 66-70
    let speed = data.slice(66, 70);
    //09角度（油门） 72-76
    let gas = data.slice(72, 76);
    //0a机油剩余量 78-80
    let oil_left = data.slice(78, 80);
    //0b报警状态开关量 82-86
    let warn_status_num = data.slice(82, 86);
    //0c发动机运行状态 88-90
    let engine_run_status = data.slice(88, 90);
    //0e油量 92-94
    let oil_count = data.slice(92, 94);
    //0f温度 96-98
    let temperature = data.slice(96, 98);
    //12试用倒计时计数
    let trail_time = data.slice(104, 108);
    //33工作模式 100-102
    let work_mode = data.slice(110, 112);
    //34控制方式 104-106
    let control_way = data.slice(114, 116);
    //35启动标志 108-110
    let start_tag = data.slice(118, 120);
    //36自启电压 112-116
    let self_start_voltage = data.slice(122, 126);
    //37自停模式 118-120
    let self_stop_mode = data.slice(128, 130);
    //38发电电压 122-126
    let electricity_voltage = data.slice(132, 136);
    //39电压等级 128-132
    let electricity_level = data.slice(138, 142);
    //3a脚踏信号模式 134-136
    let foot_signal_model = data.slice(144, 146);
    //3b激活状态
    let active_statue = data.slice(148, 150);
    //3c燃料类型
    let oil_type = data.slice(152, 154);
    
//    console.log("01电压："+voltage);
//    console.log("02功率："+power);
//    console.log("03频率："+frequency);
//    console.log("04单次运行时间："+single_run_time);
//    console.log("05累计运行时间："+total_run_time);
//    console.log("06单次发电功率："+single_electricity_power);
//    console.log("07累计发电功率："+total_electricity_power);
//    console.log("08转速："+speed);
//    console.log("09油门："+gas);
//    console.log("0a机油剩余量："+oil_left);
//    console.log("0b报警状态开关量："+warn_status_num);
//    console.log("0c发动机运行状态："+engine_run_status);
//    console.log("0e油量："+oil_count);
//    console.log("0f温度："+temperature);
//    console.log("12倒计时："+trail_time);
//    console.log("33工作模式："+work_mode);
//    console.log("34控制方式："+control_way);
//    console.log("35启动标志："+start_tag);
//    console.log("36自启电压："+self_start_voltage);
//    console.log("37自停模式："+self_stop_mode);
//    console.log("38发电电压："+electricity_voltage);
//    console.log("39电压等级："+electricity_level);
//    console.log("3a脚踏信号模式："+foot_signal_model);
//    console.log("3b激活状态："+active_statue);
//    console.log("3c燃料类型："+oil_type);
//    console.log("\n\n\n");
//
    voltage = reverseStrByEveryTwoLetter(voltage);
    power = reverseStrByEveryTwoLetter(power);
    frequency = reverseStrByEveryTwoLetter(frequency);
    single_run_time = reverseStrByEveryTwoLetter(single_run_time);
    total_run_time = reverseStrByEveryTwoLetter(total_run_time);
    single_electricity_power = reverseStrByEveryTwoLetter(single_electricity_power);
    total_electricity_power = reverseStrByEveryTwoLetter(total_electricity_power);
    speed = reverseStrByEveryTwoLetter(speed);
    gas = reverseStrByEveryTwoLetter(gas);
    oil_left = reverseStrByEveryTwoLetter(oil_left);
    warn_status_num = reverseStrByEveryTwoLetter(warn_status_num);
    engine_run_status = reverseStrByEveryTwoLetter(engine_run_status);
    oil_count = reverseStrByEveryTwoLetter(oil_count);
    temperature = reverseStrByEveryTwoLetter(temperature);
    trail_time = reverseStrByEveryTwoLetter(trail_time);
    work_mode = reverseStrByEveryTwoLetter(work_mode);
    control_way = reverseStrByEveryTwoLetter(control_way);
    start_tag = reverseStrByEveryTwoLetter(start_tag);
    self_start_voltage = reverseStrByEveryTwoLetter(self_start_voltage);
    self_stop_mode = reverseStrByEveryTwoLetter(self_stop_mode);
    electricity_voltage = reverseStrByEveryTwoLetter(electricity_voltage);
    electricity_level = reverseStrByEveryTwoLetter(electricity_level);
    foot_signal_model = reverseStrByEveryTwoLetter(foot_signal_model);
    active_statue = reverseStrByEveryTwoLetter(active_statue);
    oil_type = reverseStrByEveryTwoLetter(oil_type);

//    console.log("反转-01电压："+voltage);
//    console.log("反转-02功率："+power);
//    console.log("反转-03频率："+frequency);
//    console.log("反转-04单次运行时间："+single_run_time);
//    console.log("反转-05累计运行时间："+total_run_time);
//    console.log("反转-06单次发电功率："+single_electricity_power);
//    console.log("反转-07累计发电功率："+total_electricity_power);
//    console.log("反转-08转速："+speed);
//    console.log("反转-09油门："+gas);
//    console.log("反转-0a机油剩余量："+oil_left);
//    console.log("反转-0b报警状态开关量："+warn_status_num);
//    console.log("反转-0c发动机运行状态："+engine_run_status);
//    console.log("反转-0e油量："+oil_count);
//    console.log("反转-0f温度："+temperature);
//    console.log("反转-12试用倒计时："+trail_time);
//    console.log("反转-33工作模式："+work_mode);
//    console.log("反转-34控制方式："+control_way);
//    console.log("反转-35启动标志："+start_tag);
//    console.log("反转-36自启电压："+self_start_voltage);
//    console.log("反转-37自停模式："+self_stop_mode);
//    console.log("反转-38发电电压："+electricity_voltage);
//    console.log("反转-39电压等级："+electricity_level);
//    console.log("反转-3a脚踏信号模式："+foot_signal_model);
//    console.log("反转-3b激活状态："+active_statue);
//    console.log("反转-3c燃料类型："+oil_type);
//    console.log("\n\n\n");
//
    
    User.currentChoosedMachineBleData.is_new_version_equipment = true;
        //3b 位非00全是激活状态
        if(active_statue === "00"){
            User.currentChoosedMachineBleData.is_active = "0";
        }else{
            User.currentChoosedMachineBleData.is_active = "1";
        }
    
    User.currentChoosedMachineBleData.trial_time = parseInt(hexUtil.hexToString(trail_time));
    User.currentChoosedMachineBleData.frequency = hexUtil.hexToString(frequency);
    User.currentChoosedMachineBleData.single_run_time = hexUtil.hexToString(single_run_time);
    User.currentChoosedMachineBleData.total_run_time = hexUtil.hexToString(total_run_time);
    User.currentChoosedMachineBleData.single_elec_power =  (parseInt(single_electricity_power,16)/10.0).toString()
    User.currentChoosedMachineBleData.total_power_power =  (parseInt(total_electricity_power,16)/10.0).toString()
    User.currentChoosedMachineBleData.oil_mass = hexUtil.hexToString(oil_left);


    let warnStatusStr =  hexUtil.hexToString(warn_status_num);
    if( User.currentChoosedMachineBleData.warn_status_tips !== warnStatusStr){
        User.currentConnectedEquipment.warning_tips = ''
      }
    User.currentChoosedMachineBleData.warn_status_tips = warnStatusStr;
    User.currentChoosedMachineBleData.engine_run_status = hexUtil.hexToString(engine_run_status);
    User.currentChoosedMachineBleData.work_mode = hexUtil.hexToString(work_mode);
    User.currentChoosedMachineBleData.control_status = hexUtil.hexToString(control_way);
    User.currentChoosedMachineBleData.start_stop_tag = hexUtil.hexToString(start_tag);
    User.currentChoosedMachineBleData.self_start_voltage =  (parseInt(self_start_voltage,16)/10.0).toString()
    User.currentChoosedMachineBleData.self_stop_mode = hexUtil.hexToString(self_stop_mode);
    User.currentChoosedMachineBleData.electricity_voltage =  (parseInt(electricity_voltage,16)/10.0).toString()
    User.currentChoosedMachineBleData.pedal_signal_model = hexUtil.hexToString(foot_signal_model);
   User.currentChoosedMachineBleData.curr_oil_count = hexUtil.hexToString(oil_count);
    User.currentChoosedMachineBleData.curr_temperature = hexUtil.hexToString(temperature);
    User.currentChoosedMachineBleData.curr_voltage_level =  (parseInt(electricity_level,16)/10.0).toString()
    User.currentChoosedMachineBleData.oil_type = hexUtil.hexToString(oil_type);

    if(parseInt(User.currentChoosedMachineBleData.engine_run_status) == 1 && parseInt(User.currentChoosedMachineBleData.control_status) == 1 &&  parseInt(User.currentChoosedMachineBleData.start_stop_tag) == 1){
        User.currentChoosedMachineBleData.is_running = true;
    }else{
        User.currentChoosedMachineBleData.is_running = false;
    }

    
    
    let voltageModel = {} as any;
    voltageModel.type = 100
    voltageModel.timestamp = Math.floor(new Date().getTime() / 1000);
    voltageModel.data =  (parseInt(voltage,16)/10.0).toString();
    voltageModel.deviceID = User.currentConnectedBlueTooth.deviceId
           
    let speedModel = {} as any;
    speedModel.timestamp = Math.floor(new Date().getTime() / 1000);
    speedModel.type = 101
    speedModel.data = hexUtil.hexToString(speed);
    speedModel.deviceID = User.currentConnectedBlueTooth.deviceId
           
    let powerModel = {} as any;
    powerModel.timestamp = Math.floor(new Date().getTime() / 1000);
    powerModel.type = 102
    powerModel.data = hexUtil.hexToString(power);
    powerModel.deviceID = User.currentConnectedBlueTooth.deviceId
           
    let gasModel = {} as any;
    gasModel.timestamp = Math.floor(new Date().getTime() / 1000);
    gasModel.type = 103
    gasModel.data = hexUtil.hexToString(gas);
    gasModel.deviceID = User.currentConnectedBlueTooth.deviceId
    
    let oilCountModel = {} as any;
    oilCountModel.timestamp = Math.floor(new Date().getTime() / 1000);
    oilCountModel.type = 104
    oilCountModel.data = hexUtil.hexToString(oil_count);
    oilCountModel.deviceID = User.currentConnectedBlueTooth.deviceId

    let runtimeModel = {} as any;
    runtimeModel.timestamp = Math.floor(new Date().getTime() / 1000);
    runtimeModel.type = 104
    runtimeModel.totalUseTime = User.currentChoosedMachineBleData.total_run_time;
    runtimeModel.singleUseTime = User.currentChoosedMachineBleData.single_run_time;
    runtimeModel.totalElc = User.currentChoosedMachineBleData.total_power_power;
    runtimeModel.singleElc = User.currentChoosedMachineBleData.single_elec_power;
    runtimeModel.deviceID = User.currentConnectedBlueTooth.deviceId

    if(User.voltageList === null){
        User.voltageList = [] as any
    }

    if(User.powerList === null){
        User.powerList = [] as any
    }

    if(User.acceleratorList === null){
        User.acceleratorList = [] as any
    }

    if(User.speedList === null){
        User.speedList = [] as any
    }

    if(User.oilCountList === null){
        User.oilCountList = [] as any
    }

    if(User.runtimeList === null){
        User.runtimeList = [] as any
    }

    User.voltageList.push(voltageModel)
    User.powerList.push(powerModel)
    User.acceleratorList.push(gasModel)
    User.speedList.push(speedModel)
    User.oilCountList.push(oilCountModel)
    User.runtimeList.push(runtimeModel)

    
//#warning TODOO 临时测试
//    User.currentChoosedMachineBleData.engine_run_status = kRandomNum(2);
//    User.currentChoosedMachineBleData.work_mode = kRandomNum(4);
//    User.currentChoosedMachineBleData.curr_voltage_level = kRandomNum(30);
//    User.currentChoosedMachineBleData.control_status = kRandomNum(2);
//    User.currentChoosedMachineBleData.self_start_voltage = [NSString stringWithFormat:"%.0f",kRandomNum(100).floatValue/10.);
//    User.currentChoosedMachineBleData.self_stop_mode = kRandomNum(10);
//    User.currentChoosedMachineBleData.electricity_voltage = [NSString stringWithFormat:"%.0f",kRandomNum(100).floatValue/10.);
//    User.currentChoosedMachineBleData.pedal_signal_model = kRandomNum(3);
//    voltageModel.data =  kRandomNum(83);
//    powerModel.data =  kRandomNum(55298);
//    gasModel.data =  kRandomNum(150);
//    oilCountModel.data =  kRandomNum(100);
//    speedModel.data =  kRandomNum(150);
//    User.currentChoosedMachineBleData.single_run_time = Math.floor(new Date().getTime() / 1000);
//    User.currentChoosedMachineBleData.total_run_time = Math.floor(new Date().getTime() / 1000);
//    User.currentChoosedMachineBleData.single_elec_power = [NSString stringWithFormat:"%.1f",kRandomNum(100000).floatValue/10.);
//    User.currentChoosedMachineBleData.total_power_power = [NSString stringWithFormat:"%.1f",kRandomNum(100000).floatValue/10.);
//
    
    
    // [EquipmentCacheDataManager addVoltageData:voltageModel);
    // [EquipmentCacheDataManager addPowerData:powerModel);
    // [EquipmentCacheDataManager addAcceleratorData:gasModel);
    // [EquipmentCacheDataManager addSpeedData:speedModel);
    // [EquipmentCacheDataManager addOilCountData:oilCountModel);


    
    User.currentChoosedMachineBleData.curr_voltage = voltageModel.data;
    User.currentChoosedMachineBleData.curr_power = powerModel.data;
    User.currentChoosedMachineBleData.curr_speed = speedModel.data;
    User.currentChoosedMachineBleData.curr_gas = gasModel.data;
    User.currentChoosedMachineBleData.curr_oil_count = oilCountModel.data;
    console.log('3.0设备')

   console.log("数据-01电压："+voltageModel.data);
   console.log("数据-02功率："+powerModel.data);
   console.log("数据-03频率："+User.currentChoosedMachineBleData.frequency);
   console.log("数据-04单次运行时间："+User.currentChoosedMachineBleData.single_run_time);
   console.log("数据-05累计运行时间："+User.currentChoosedMachineBleData.total_run_time);
   console.log("数据-06单次发电功率："+User.currentChoosedMachineBleData.single_elec_power);
   console.log("数据-07累计发电功率："+User.currentChoosedMachineBleData.total_power_power);
   console.log("数据-08转速："+speedModel.data);
   console.log("数据-09油门："+gasModel.data);
   console.log("数据-0a机油剩余量："+User.currentChoosedMachineBleData.oil_mass);
   console.log("数据-0b报警状态开关量："+User.currentChoosedMachineBleData.warn_status_tips);
   console.log("数据-0c发动机运行状态："+User.currentChoosedMachineBleData.engine_run_status);
   console.log("数据-0e油量："+User.currentChoosedMachineBleData.curr_oil_count);
   console.log("数据-0f温度："+User.currentChoosedMachineBleData.curr_temperature);
   console.log("数据-12试用倒计时："+User.currentChoosedMachineBleData.trial_time);
   console.log("数据-33工作模式："+User.currentChoosedMachineBleData.work_mode);
   console.log("数据-34控制方式："+User.currentChoosedMachineBleData.control_status);
   console.log("数据-35启动标志："+User.currentChoosedMachineBleData.start_stop_tag);
   console.log("数据-36自启电压："+User.currentChoosedMachineBleData.self_start_voltage);
   console.log("数据-37自停模式："+User.currentChoosedMachineBleData.self_stop_mode);
   console.log("数据-38发电电压："+User.currentChoosedMachineBleData.electricity_voltage);
   console.log("数据-3a脚踏信号模式："+User.currentChoosedMachineBleData.pedal_signal_model);
   console.log("数据-39电压等级："+(parseInt(User.currentChoosedMachineBleData.curr_voltage_level,16)/10.0).toString())
   console.log("数据-3b激活状态："+User.currentChoosedMachineBleData.is_active);
   console.log("数据-3c燃料类型："+User.currentChoosedMachineBleData.oil_type);
   console.log("\n\n\n");
    
 
}

///把字符串每两位一反转
function reverseStrByEveryTwoLetter(word:string){
   let res = ''
   for(let i=0; i<word.length / 2.; i++){
      let locate = i * 2
      let length = i * 2 + 2
      if(locate < word.length - 1){
        let tmp = word.slice(locate,length)
        res = tmp + res
      }else{
        length = word.length - 1 - locate
        let tmp = word.slice(locate,length)
        res = tmp + res
      }
   }
   return res
}

///获取所有变量数据类型
function getVariableRangeDataType(data:string){
    if(data.length === 10){
        return data.slice(0,2)
    }else{
        return data
    }
}

///获取所有变量数据的最小值
function getVariableRangeMinValueType(data:string){
    if(data.length === 10){
        let str = data.slice(2,6)
        let res = reverseStrByEveryTwoLetter(str)
        return res
    }else{
        return data
    }
}

///获取所有变量数据的最大值
function getVariableRangeMaxValueType(data:string){
    if(data.length === 10){
        let str = data.slice(6,10)
        let res = reverseStrByEveryTwoLetter(str)
        return res
    }else{
        return data
    }
}

///反转字符串
function reverseString(str:string){
    let reversedStr = '';
    for (let i = str.length - 1; i >= 0; i--) {
        reversedStr += str[i];
    }
    return reversedStr;
}

///把使能变量状态数据转换成数组
function getDeviceDataStatus(data:string){
    let array = [] as any;
    if(data.length === 88){
        for(let i=0; i<data.length; i+=4){
            let tmp = data.slice(i,i+4)
            array.push(tmp)
        }
        return array
    }
    return array
}
///取出具体使能的状态
function getDataStausForm(data:string){
    let status = '00'
    if(data.length === 4){
        status = data.slice(2,4)
        return status
    }else{
        return data
    }
}


///转换得到发送的蓝牙命令
function getCMDStr(commend:string){

    switch (commend) {
        case 'BLE_COMMEND_SET_START_STOP_TAG_START'://设置启停标志-启动
            return "A50302003501E00D0A";
        case 'BLE_COMMEND_SET_START_STOP_TAG_STOP'://设置启停标志-停止
            return "A50302003500DF0D0A";
        case 'BLE_COMMEND_SET_CONTROL_STATUS_AUTO'://设置控制状态-自动
            return "A50302003401DF0D0A";
        case 'BLE_COMMEND_SET_CONTROL_STATUS_MANUAL'://设置控制状态-手动
            return "A50302003400DE0D0A";
        case 'BLE_COMMEND_SET_WORK_MODE_SLEEP'://设置工作模式-睡眠
            return "A50302003300DD0D0A";
        case 'BLE_COMMEND_SET_WORK_MODE_STANDARD'://设置工作模式-标准
            return "A50302003301DE0D0A";
        case 'BLE_COMMEND_SET_WORK_MODE_POWERFUL'://设置工作模式-强劲
            return "A50302003302DF0D0A";
        case 'BLE_COMMEND_SET_WORK_MODE_CHARGE'://设置工作模式-充电
            return "A50302003303E00D0A";
        case 'BLE_COMMEND_SET_SELF_START_VOLTAGE'://设置自启电压
            return "A5 03 03 00 36 VL VH s 0D 0A";
        case 'BLE_COMMEND_SET_VOLTAGE_LEVEL'://设置电压等级
            return "A5 03 03 00 39 VL VH s 0D 0A";
        case 'BLE_COMMEND_SET_SELF_STOP_MODE_0'://设置自停模式-0
            return "A50302003700E10D0A";
        case 'BLE_COMMEND_SET_SELF_STOP_MODE_1'://设置自停模式-1
            return "A50302003701E20D0A";
        case 'BLE_COMMEND_SET_SELF_STOP_MODE_2'://设置自停模式-2
            return "A50302003702E30D0A";
        case 'BLE_COMMEND_SET_SELF_STOP_MODE_3'://设置自停模式-3
            return "A50302003703E40D0A";
        case 'BLE_COMMEND_SET_SELF_STOP_MODE_4'://设置自停模式-4
            return "A50302003704E50D0A";
        case 'BLE_COMMEND_SET_SELF_STOP_MODE_5'://设置自停模式-5
            return "A50302003705E60D0A";
        case 'BLE_COMMEND_SET_SELF_STOP_MODE_6'://设置自停模式-6
            return "A50302003706E70D0A";
        case 'BLE_COMMEND_SET_SELF_STOP_MODE_7'://设置自停模式-7
            return "A50302003707E80D0A";
        case 'BLE_COMMEND_SET_RESETTING'://恢复出厂设置
            return "A50A0000AF0D0A";
        case 'BLE_COMMEND_SET_GENERATE_VOLTAGE'://设置发电电压
            return "A5 03 03 00 38 VL VH s 0D 0A";
        case 'BLE_COMMEND_SET_PEDAL_SIGNAL_DISABLE'://设置脚踏信号模式-禁止
            return "A50302003A00E40D0A";
        case 'BLE_COMMEND_SET_PEDAL_SIGNAL_USE'://设置脚踏信号模式-使能
            return "A50302003A01E50D0A";
        case 'BLE_COMMEND_SET_PEDAL_SIGNAL_AUTO'://设置脚踏信号模式-自动检测
            return "A50302003A02E60D0A";
        case 'BLE_COMMEND_GET_RUNTIME_DATA'://读实时数据(所有变量）
            return "A5010000A60D0A";
        case 'BLE_COMMEND_GET_VARIABLE_RANGE_DATA'://读取所有范围
            return "A5020000A70D0A";
        case 'BLE_COMMEND_GET_VOLTAGE_LEVEL': //读取电压等级
            return "A5040000A90D0A";
        case 'BLE_COMMEND_GET_BLE_VERSION'://读取蓝牙版本号
            return "A5060000AB0D0A";
        case 'BLE_COMMEND_GET_DEVICE_DATA_STATUS'://读取使能变量
            return "A50B0000B00D0A";
        case 'BLE_COMMEND_ACTIVE_EQUIPMENT'://激活设备
            return "A50302003B02E70D0A";
        case 'BLE_COMMEND_GET_UNIONID'://得到蓝牙唯一id
            return "A50C0000B10D0A";
        case 'BLE_COMMEND_GET_CONTROL_VERSION'://得到控制器版本号
            return "A5050000AA0D0A";
        case 'BLE_COMMEND_CLEAR_OIL_WARNING'://清除机油报警
            return "A50711000000000000000000000000000000000000BD0D0A";
        case 'BLE_COMMEND_SET_OIL_TYPE_LNG'://设置LNG燃气类型
            return "A50302003C01E70D0A";
        case 'BLE_COMMEND_SET_OIL_TYPE_OIL'://设置燃油类型
            return "A50302003C00E60D0A";
        default:
            return ""; 
        }
    }
    
    ///重置所有数据
    function resetAllTempString(){
        User.bluetoothCallbackData.string_voltage_variable = "";
        User.bluetoothCallbackData.string_voltage_level = "";
        User.bluetoothCallbackData.string_ble_data_status_data = "";
        User.bluetoothCallbackData.string_ble_version_data = "";
        //获取实时数据
        User.bluetoothCallbackData.string_runtime_data = "";
        User.bluetoothCallbackData.string_control_version_data = "";
    }

    ///判断是否试用设备是否已到期
   export  function judgeCanSendCmdWhenTrailTimeIsOvered(){


        console.log('isConnectedBlueTooth:'+User.isConnectedBlueTooth)
        console.log('is_new_version_equipment:'+User.currentChoosedMachineBleData.is_new_version_equipment)
        console.log('trial_time:'+User.currentChoosedMachineBleData.trial_time)
        console.log('actStatus:'+User.choosedShowDetailEquip.actStatus)


       if(User.isConnectedBlueTooth && User.currentChoosedMachineBleData.is_new_version_equipment && User.currentChoosedMachineBleData.trial_time <= 0 && ( User.choosedShowDetailEquip.actStatus === false ||  User.choosedShowDetailEquip.actStatus === null)){

        uni.showModal({
            title: '设备试用结束，请激活后继续使用设备。',
            showCancel:false,
            confirmText:'确定',
            success: function (res) {
                
            },

        });
        return false;
       }else{
        return true
       }
    }


   



    